﻿using Common.Contract.Authentication.Record;
using Common.Contract.Paging;
using Common.Contract.ProductHandbook.Record;
using Common.Contract.ProductHandbook.Request;
using Common.Contract.ProductHandbook.Response;
using Common.Contract.SystemConfig.Record;
using Common.Service.Command;
using CrossSelling.Model.Interface;
using CrossSelling.Model.Model;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;

namespace CrossSelling.Core.ServiceCommand
{
    public class RetrieveViewChecklistCustomerByCcidServiceCommand : ProviderServiceCommand<IProviderFactory, RetrieveViewChecklistCustomerByCcidRequest, RetrieveViewChecklistCustomerByCcidResponse>
    {
        #region Properties
        public UserRecord Dao;
        public FilterRecord TextSearch;
        public int pageSize = 10, pageIndex = 0, totalRecord = 0;
        public DateTime? FromDate;
        private List<ChecklistDocumentRecord> ListCustomer = new List<ChecklistDocumentRecord>();
        public int Ccid;
        CultureInfo cul = CultureInfo.GetCultureInfo("vi-VN");   // try with "en-US"
        #endregion
        public RetrieveViewChecklistCustomerByCcidServiceCommand(IProviderFactory providers) : base(providers) { }
        protected override void Validate(RetrieveViewChecklistCustomerByCcidRequest request)
        {
            if (request == null) throw new ArgumentNullException("request");
            if (!request.Header.ApplicationKey.HasValue || request.Header.ApplicationKey == Guid.Empty)
            {
                throw new ArgumentException("ApplicationKey must be set", "request");
            }
            if (string.IsNullOrEmpty(request.Ccid))
            {
                throw new ArgumentException("Checklist Customer Id must be set", "request");
            }
            else
            {
                Int32.TryParse(request.Ccid, out Ccid);
            }

            if (request.CurrentUser == null)
            {
                throw new ArgumentException("MSBO Dao must be set", "request");
            }
            else
            {
                Dao = Request.CurrentUser;
            }
            TextSearch = Request.Filter;
            if (Request.Page != null)
            {
                pageIndex = Request.Page.PageIndex ?? 0;
                pageSize = Request.Page.PageSize ?? 0;
            }
        }

        protected override bool OnExecute(RetrieveViewChecklistCustomerByCcidResponse response)
        {
            bool isSuccess = true;
            string errorMessage = string.Empty;
            try
            {
                List<ChecklistDocumentRecord> records = new List<ChecklistDocumentRecord>();

                ChecklistCustomer checklistCustomer = Providers.ProductHandbooks.RetrieveCustomerChecklistByCcid(Ccid, Dao);

                if (checklistCustomer != null || checklistCustomer.Ccid == 0)
                {
                    List<CheckListDocument> listCustomer = new List<CheckListDocument>();
                    listCustomer = Providers.ProductHandbooks.RetrieveListDocumentChecklist(out totalRecord, checklistCustomer, Dao, TextSearch, Request.Lang, pageSize, pageIndex);
                    Dictionary<int, ChecklistDocumentByCustomer> listDocumentByCcid = Providers.ProductHandbooks.RetrieveListDocumentByCcid(out totalRecord, Ccid, Dao, TextSearch, Request.Lang, pageSize, pageIndex).ToDictionary(x => x.DocumentId);
                    if (listCustomer != null)
                    {
                        records = listCustomer.Where(x => listDocumentByCcid.ContainsKey(x.ChecklistDocId)).Select(x => new ChecklistDocumentRecord
                        {
                            DocumentGroup = x.DocumentGroup,
                            DocumentGroupCategory = x.DocumentGroupCategory,
                            isCheck = listDocumentByCcid[x.ChecklistDocId].Status == 0 ? false : true,
                            Document = x.Document,
                            Original = x.Original,
                            Copy = x.Copy,
                            Duplicate = x.Duplicate,
                            Note = x.Note,
                            PictureDoc = x.PictureDocument,
                            Id = x.ChecklistDocId.ToString()
                        }).ToList();
                    }
                }
                response.Records = records;
                response.Page = new PageRecord
                {
                    PageIndex = pageIndex,
                    PageSize = pageSize,
                    TotalRecords = totalRecord
                };
            }
            catch (Exception e)
            {
                isSuccess = false;
                errorMessage = e.Message;

            }

            response.ErrorMessage = errorMessage;
            return isSuccess;
        }
    }
}
